package com.aaa.controller;

import com.aaa.entity.User;
import com.aaa.service.impl.UserServiceImpl;
import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;



@Controller
@RequestMapping("user")
public class UserController {
@Resource
    UserServiceImpl userService;
    @RequestMapping("sel")
    @ResponseBody
    public List<User>sel(User user){
        List<User> list=userService.sel(user);
        return list;

    }
    @RequestMapping("list")
    @ResponseBody
    public List<User>list( ){
        List<User> list=userService.list();
        return list;

    }
    @RequestMapping("insertUser")
    @ResponseBody
    public boolean insertUser(User user){
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
            user.setPassword( encoder.encode(user.getPassword()));
        boolean list=userService.save(user);
        return list;

    }
//    @RequestMapping("updateById")
//    @ResponseBody
//    public  boolean updateById(User user){
//        return userService.updateP(user);
//    }
    @RequestMapping("updatePhoto")
    @ResponseBody
    public   int updatePhoto(MultipartFile image,Integer uid,String username,String sex,String address,String email, HttpServletRequest req){
        System.out.println("测试："+uid);
        System.out.println("ceshi:"+image);
        String filename=image.getOriginalFilename();
        System.out.println("测试："+filename);
        String sName="";
        if (null!=filename&&!"".equals(filename)){
            String filePath=req.getServletContext().getRealPath("/static/img/footer/");
            sName="/static/img/footer/"+filename;
//            System.out.println("测试："+sName);
        }
        int rs = userService.updatePhoto(uid,username,sex,address,email, sName);
        System.out.println(rs+"1111111111111111111111  ");
        return rs;
    }

}
